using System.Collections.Concurrent;

namespace OracleInternal.Common
{
	internal class XmlSchemaPool
	{
		private object m_sync;

		private ConcurrentDictionary<string, CachedSchemaWithId> m_schemaUrlCache;

		private ConcurrentDictionary<byte[], CachedSchemaWithUrl> m_schemaIdCache;

		private int m_maxCacheSize;

		internal CachedSchemaWithId this[string url]
		{
			get
			{
				try
				{
					if (url == null)
					{
						return null;
					}
					url = url.Trim();
					if (url.Length == 0)
					{
						return null;
					}
					CachedSchemaWithId value = null;
					if (m_schemaUrlCache.TryGetValue(url, out value))
					{
						return value;
					}
					return null;
				}
				catch
				{
					return null;
				}
			}
			set
			{
				if (url == null)
				{
					return;
				}
				url = url.Trim();
				if (url.Length == 0 || m_schemaUrlCache.Count >= m_maxCacheSize)
				{
					return;
				}
				lock (m_sync)
				{
					if (m_schemaUrlCache.Count < m_maxCacheSize && !m_schemaUrlCache.TryAdd(url, value))
					{
						m_schemaUrlCache.TryUpdate(url, value, null);
					}
				}
			}
		}

		internal CachedSchemaWithUrl this[byte[] id]
		{
			get
			{
				try
				{
					if (id == null)
					{
						return null;
					}
					CachedSchemaWithUrl value = null;
					if (m_schemaIdCache.TryGetValue(id, out value))
					{
						return value;
					}
					return null;
				}
				catch
				{
					return null;
				}
			}
			set
			{
				if (id == null || m_schemaIdCache.Count >= m_maxCacheSize)
				{
					return;
				}
				lock (m_sync)
				{
					if (m_schemaIdCache.Count < m_maxCacheSize && !m_schemaIdCache.TryAdd(id, value))
					{
						m_schemaIdCache.TryUpdate(id, value, null);
					}
				}
			}
		}

		internal XmlSchemaPool(int maxCacheSize)
		{
			m_sync = new object();
			m_maxCacheSize = maxCacheSize;
			m_schemaIdCache = new ConcurrentDictionary<byte[], CachedSchemaWithUrl>();
			m_schemaUrlCache = new ConcurrentDictionary<string, CachedSchemaWithId>();
		}

		internal bool Contains(string url)
		{
			if (m_schemaUrlCache.ContainsKey(url))
			{
				return true;
			}
			return false;
		}

		internal bool Contains(byte[] id)
		{
			if (m_schemaIdCache.ContainsKey(id))
			{
				return true;
			}
			return false;
		}
	}
}
